/**
 * <p>Copyright:Copyright(c) 2019</p>
 * <p>Company:Professional</p>
 * <p>Package:org.cesgroup.course.web</p>
 * <p>File:Ch003Controller.java</p>
 * <p>类更新历史信息</p>
 * @todo chanlong(陈龙) 创建于 Sep 10, 2019 4:36:09 PM
 */
package com.ces.controller;

import cn.hutool.core.io.FileUtil;
import com.ces.converter.Converter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@RestController
public class ConvertController {

	// 注入“Converter”bean
	@Autowired
	private Converter converter;

	@PostMapping("/word/2/html")
	public Object word2Html(final MultipartFile file) {
		try {
			// 启动责任链
			String filepath = (String)converter.process(file.getBytes(), FileUtil.extName(file.getOriginalFilename()));
			byte[] data = FileUtil.readBytes(filepath);
			HttpHeaders headers = new HttpHeaders();
			headers.setContentDispositionFormData("attachment", "demo.html");   
	        headers.setContentType(MediaType.TEXT_HTML); 
	        return new ResponseEntity<byte[]>(data, headers, HttpStatus.OK); 
		} catch (Throwable e) {
			throw new RuntimeException(e);
		}
	}
	

}
